/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.secure.web.group;

import java.util.Arrays;
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.fyx.linker.web.LinkerLocal;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.secure.controll.SecureGroupFacadeLocal;
import org.fyx.secure.controll.SecureRoleFacadeLocal;
import org.fyx.secure.ejb.SecureRoles;
import org.fyx.secure.entity.SecureGroup;
import org.fyx.secure.entity.SecureRole;
import org.fyx.util.FyxUtility;

/**
 *
 * @author xljan
 */
@ManagedBean
@SessionScoped
public class SecureGroupEdit {

    private SecureGroup group;
    private SecureRole[] selectedRoles;
    @EJB
    private SecureGroupFacadeLocal secureGroupFacade;
    @EJB
    private SecureRoleFacadeLocal secureRoleFacade;
    @EJB
    private MulaLocal mula;
    @EJB
    private LinkerLocal linker;

    @PostConstruct
    public void postConstruct(){
        FyxUtility.checkRole(SecureRoles.GROUP_MANAGER);
    }
    
    public SecureGroup getGroup() {
        return group;
    }

    public SecureRole[] getSelectedRoles() {
        return selectedRoles;
    }

    public void setSelectedRoles(SecureRole[] selectedRoles) {
        this.selectedRoles = selectedRoles;
    }

    public void setGroup(SecureGroup group) {
        this.group = group;
    }

    public void init(int idGroup) {
        group = secureGroupFacade.find(idGroup);
        selectedRoles = group.getSecureRoleCollection().toArray(
                new SecureRole[group.getSecureRoleCollection().size()]);
    }

    public void edit() {
        group.getSecureRoleCollection().clear();
        group.getSecureRoleCollection().addAll(Arrays.asList(selectedRoles));
        secureGroupFacade.edit(group);
        linker.recreateMenuModel();
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
                mula.get(mula.get("Group was saves sucessfull.")), null);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public Collection<SecureRole> getAllRoles() {
        return secureRoleFacade.findAll();
    }
}
